package project.active;

import java.util.ArrayList;
import java.util.Date;

import model.pojo.Employee;
import project.dao.EmployeeDAO;
import project.dao.PrCompositionDAO;
import project.dao.ProjectDAO;
import project.model.PrComposition;
import project.model.PrProject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import approve.action.R;

public class Lupdatetesterlist extends Activity {
	Bundle language;
	String lang="EN",database;
	PrProject pr =new PrProject();
	ProjectDAO prodao=new ProjectDAO();
	EmployeeDAO emdao=new EmployeeDAO();
	Employee emp=new Employee();
	PrCompositionDAO pcdao=new PrCompositionDAO();
	
	TextView proinfo,title;
	Button btgocodelist,btsave;
	ListView lstemcode;
	TextView logout,username;
	//String[] idCheck=null;
	ArrayList<Employee> emlist;
	Lpopupcoder adapterEm ;
	
	public String getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
		}
		return database;
	}
	
	public String getLang() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			lang = extras.getString("lang");
		}
		return lang;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lupdatetesterlist);
		language = getIntent().getExtras().getBundle(getLang());
		
		title=(TextView)findViewById(R.id.l_tv_title_addtestlist);
		title.setText(language.getString("add_tester"));
		proinfo=(TextView)findViewById(R.id.l_tv_upttproinfo);
		btgocodelist=(Button)findViewById(R.id.l_bt_upttgoCodelist);
		btsave=(Button)findViewById(R.id.l_bt_upttsave);
		lstemcode=(ListView)findViewById(R.id.l_lv_upttem);
		
		btgocodelist.setText(language.getString("tester_team"));
		btsave.setText(language.getString("l_addtesterteam"));
		
		Intent i=this.getIntent();
		pr=(PrProject)i.getSerializableExtra("project");		
		emp=(Employee)i.getSerializableExtra("em");
		//idCheck=(String[])i.getSerializableExtra("lstcheck");
		
		logout=(TextView)findViewById(R.id.logout);
		username=(TextView)findViewById(R.id.username);
		logout.setText(language.getString("Logout"));
		
		logout.setOnClickListener(listenerLogout);
		username.setText(emp.getUserName());
		
		emlist=loadEmp();
		
		proinfo.setText(pr.getIdProject()+" - "+pr.getNameProject());
		btgocodelist.setOnClickListener(gocodelist);
		btsave.setOnClickListener(savecoder);
		
		if(emlist.size()>0){
			adapterEm = new Lpopupcoder(this,emlist);
			lstemcode.setAdapter(adapterEm);
		}
	}
	
private OnClickListener savecoder= new OnClickListener() {
		
		
		public void onClick(View v) {
			// TODO Auto-generated method stub

//			idCheck=adapterEm.getIdCheck();
//			if(idCheck!=null){
//				System.out.println("11111");
//				for(int i=0;i<idCheck.length;i++){
//					if(idCheck[i]!=null){
//						PrComposition pc=new PrComposition();
//						Date d=new Date();
//						pc.setIdComposition(""+d.getTime());
//						pc.setIdEmployee(idCheck[i]);
//						pc.setIdProject(pr.getIdProject());
//						if(pcdao.checkExitInsertTester(getDatabase(), pc)==false){
//							pcdao.insertTester(getDatabase(), pc);
//						}else{
//							pcdao.updatePosTest(getDatabase(), pc.getIdEmployee(),pc.getIdProject());
//						}
//						
//					}
//					
//				}
//				Intent i= new Intent(Lupdatetesterlist.this,Lvtesterlist.class);
//				i.putExtras(getIntent().getExtras());
//				i.putExtra("lang", getLang());
//				i.putExtra("database", getDatabase());
//				i.putExtra("project", pr);
//				i.putExtra("em",emp);
//				
//				startActivity(i);
//			}
			
			int k=adapterEm.getK();
			if(k>0){
				String sin="";
				ArrayList<String> lst= adapterEm.getLstcheck();
				Date d;
				for(int i=0;i<lst.size();i++){					
					d=new Date();
					if(pcdao.checkExitInsertTesterMulti(getDatabase(),pr.getIdProject(),lst.get(i))==false){
						if(sin.equals("")){
							sin="('"+d.getTime()+"','"+pr.getIdProject()+"','"+lst.get(i)+"',0,0)";
						}else{
							sin+=",('"+d.getTime()+"','"+pr.getIdProject()+"','"+lst.get(i)+"',0,0)";
						}
					}else{
						pcdao.updatePosTest(getDatabase(), lst.get(i),pr.getIdProject());
					}
				
				}
				
				if(!sin.equals("")){
					String sql="insert into pr_composition (IDComposition,IDProject,IDEmployee,Position,Status) values"+sin+"";
//					if(pcdao.insertComMulti(getDatabase(), sql)>0)
//						System.out.println("ok");
//					else{
//						System.out.println("not insert");
//					}
					pcdao.insertComMulti(getDatabase(), sql);
				}
				
				Intent i= new Intent(Lupdatetesterlist.this,Lvtesterlist.class);
				i.putExtras(getIntent().getExtras());
				i.putExtra("lang", getLang());
				i.putExtra("database", getDatabase());
				i.putExtra("project", pr);
				i.putExtra("em",emp);
				
				startActivity(i);
			}
			
		}
	};
	
	public ArrayList<Employee> loadEmp(){
		ArrayList<Employee> lst=new ArrayList<Employee>();
		lst=emdao.getAllEmployee(getDatabase());
		return lst;
	}
	
	private OnClickListener gocodelist= new OnClickListener() {
		
	
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i= new Intent(Lupdatetesterlist.this,Lvtesterlist.class);
			i.putExtras(getIntent().getExtras());
			i.putExtra("lang", getLang());
			i.putExtra("database", getDatabase());
			i.putExtra("em",emp);
			i.putExtra("project", pr);
			startActivity(i);
		}
	};
	
	private OnClickListener listenerLogout=new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i=new Intent(Lupdatetesterlist.this, LogoutActivity.class);
			i.putExtra("database", getDatabase());
			startActivity(i);
		}
	};
}
